var PGMath = (function() {
    var self = this;
    this.gcd = function(x, y) {
        if (x < y) {
            var t = x;
            x = y;
            y = t;
        }
        if (y == 0) return x;
        if (x % y == 0) return y;
        return self.gcd(y, x % y);
    };
    this.lcm = function(x, y) {
        return x * y / self.gcd(x, y);
    };
    this.auc = function(p, x, y, r) {
        var n = self.gcd(r, y);
        var m = self.lcm(x, y / n);
        var z = Math.floor(p / m);
        var t = p / x;
        var a = z * m / x;
        var b = (z + 1) * m / x;
        if (a == 0) return b;
        return ((2 * t) - a) < b ? a : b;
    };
    return this;
})();

var nrate1 = parseInt($('#points_base1').html());

function gamepoint_convert1() {
    //充值点数？
    if ($('#totel_number1').size() < 1) return;
    //游戏点数？
    if ($('#points_total1').size() < 1) return;
    //游戏点数比率？
    //$('#points_total').attr('rate', nrate1);
    //如果数据获取不到返回
    if (typeof _GPConf != 'object' || !_GPConf.rate || !_GPConf.name) return;
    //获取点数为整数
    var x = parseInt($('#totel_number1').text());
    //如果点数乘比率不是100的倍数返回提示，如果是返回充值的数量

    var returnfee = discount($(".input-amount-qrcode").val(), count_fee);
    var nrate1 = isNaN(returnfee[2]) ? 0 : returnfee[2];
    if (x * _GPConf.rate % 100 != 0) {
        var d = PGMath.auc(x, nrate1, 100, _GPConf.rate);

        $('#points_total1').html('<span class="orange">无法整除，请修改为 ' + d + ' 元</span>');
    } else {
        $('#points_total1').html('<span class="orange">' + Math.floor(x * _GPConf.rate / 100).toString() + '</span> ' + _GPConf.name);
    }

    GamePointLoading();

}

var InitGamePointLoaded = false;

function GamePointLoading() {
    if (InitGamePointLoaded) return;
    InitGamePointLoaded = true;
    var n = PGMath.gcd(100, _GPConf.rate);
    _GPConf._point = 100 / n;
    _GPConf._gamepoint = _GPConf.rate / n;
    $('#show_game_number1').html('(' + _GPConf.rate / 100 + ' ' + _GPConf.name + '/' + +_GPConf.name + ')');
    if (_gameType == 24) {
        $('#show_game_number1').hide();
    }
    // 若充值币种不是巨人点
    if (parseInt(_gameType)) {
        $('#discount_tips table').append($('<tr>', {
            html: $('<td>', {
                html: '<strong>' + _GPConf._point + ' 巨人点数 = ' + _GPConf._gamepoint + ' ' + _GPConf.name + '</strong>',
                colspan: 3
            })
        }));
    }
}

function gamepoint_convert_zzb() {
    //如果数据获取不到返回
    if (typeof _GPConf != 'object' || !_GPConf.rate || !_GPConf.name) return;
    var buy_number = $(".input-amount-qrcode").val();
    if (buy_number > limit) {
        $('#points_total1').html('<span class="orange">超过' + limit + '份限制，请重新填写！</span>');
        $(".label-option span").text(0);
        return;
    }
    var totelnumber = parseInt(buy_number) * _GPConf.rate;
    var unitnumber = totelnumber.toFixed(2);
    unitnumber = isNaN(unitnumber) ? 0 : unitnumber;
    $('#totel_number1').html(unitnumber);
    $('#points_total1').html('<span class="orange">' + unitnumber + ' 元</span>');
    $(".label-option span").text(unitnumber);
}